import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import store from '@/store/index.js'
import { Toast } from 'vant'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "view" */ '@/views/Home.vue')
  },
  {
    path: '/listview',
    name: 'ListView',
    component: () => import(/* webpackChunkName: "view" */ '@/views/ListView.vue')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import(/* webpackChunkName: "view" */ '@/views/Search.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "view" */ '@/views/Login.vue')
  },
  {
    path: '/me',
    name: 'Me',
    // 用户点击我的之前，先判断是否登录，未登录就跳转至登录页面
    beforeEnter: (to, from, next) => { 
      // console.log(store.state.user);
      if (store.state.user.isLogin) {
        next()
      } else {
        Toast.fail('请先登录')
        next('/login')
      }
    },
    component: () => import(/* webpackChunkName: "view" */ '@/views/Me.vue')
  }
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

export default router
